package com.gf.user.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.gf.base.type.Activity;
import com.gf.base.type.BloodType;
import com.gf.base.type.LicenseState;
import com.gf.base.type.Role;
import com.gf.base.type.SafeFactor;
import com.gf.base.type.Sex;
import com.gf.base.type.VIP;
import com.gf.common.util.DateFormater;
import com.gf.user.model.User;

public class UserMapper implements RowMapper<User> {

	private final static String COLUME_ID = "id";
	private final static String COLUME_NICKNAME = "nickname";
	private final static String COLUME_EMAIL = "email";
	private final static String COLUME_CELLPHONE = "cellphone";
	private final static String COLUME_PASSWORD = "password";
	private final static String COLUME_IDNUMBER = "idNumber";
	private final static String COLUME_REALNAME = "realName";
	private final static String COLUME_PORTRAIT = "portrait";
	private final static String COLUME_LAST_LOGIN_IP = "lastLoginIp";
	private final static String COLUME_LAST_LOGIN_TIME = "lastLoginTime";
	private final static String COLUME_CREATETIME = "createtime";
	private final static String COLUME_IDPATH = "idPath";
	private final static String COLUME_LICENSEPATH = "licensePath";
	private final static String COLUME_SEX = "sex";
	private final static String COLUME_LICENSESTATE = "licenseState";
	private final static String COLUME_ACTIVITY = "activity";
	private final static String COLUME_ROLE = "role";
	private final static String COLUME_MAJOR = "major";
	private final static String COLUME_EXPERIENCE = "experience";
	private final static String COLUME_INCOME = "income";
	private final static String COLUME_BIRTHDAY = "birthday";
	private final static String COLUME_BLOODTYPE = "bloodtype";
	private final static String COLUME_SAVEFACTORY = "safeFactor";
	private final static String COLUME_VIP = "vip";
	private final static String COLUME_CARECOUNT="careCount";
	private final static String COLUME_FANCOUNT="fansCount";

	private final static UserMapper MAPPER = new UserMapper();

	public final static UserMapper getMapper() {
		return MAPPER;
	}

	@Override
	public User mapRow(ResultSet resultSet, int row) throws SQLException {
		User user = new User();

		user.setId(resultSet.getLong(COLUME_ID));
		user.setNickname(resultSet.getString(COLUME_NICKNAME));
		user.setEmail(resultSet.getString(COLUME_EMAIL));
		user.setCellphone(resultSet.getString(COLUME_CELLPHONE));
		user.setPassword(resultSet.getString(COLUME_PASSWORD));
		user.setIdNumber(resultSet.getString(COLUME_IDNUMBER));
		user.setRealName(resultSet.getString(COLUME_REALNAME));
		user.setLastLoginIp(resultSet.getString(COLUME_LAST_LOGIN_IP));
		user.setLastLoginTime(resultSet.getTimestamp(COLUME_LAST_LOGIN_TIME));
		user.setCreatetime(resultSet.getTimestamp(COLUME_CREATETIME));
		user.setCreatetimeString(DateFormater.changeToString(resultSet.getTimestamp(COLUME_CREATETIME),"yyyy-MM-dd HH:mm"));
		user.setIdPath(resultSet.getString(COLUME_IDPATH));
		user.setLicensePath(resultSet.getString(COLUME_LICENSEPATH));
		user.setPortrait(resultSet.getString(COLUME_PORTRAIT));
		user.setSex(Sex.valueOf(resultSet.getString(COLUME_SEX)));
		user.setStringsex(Sex.valueOf(resultSet.getString(COLUME_SEX)).getValue());
		user.setLicenseState(LicenseState.valueOf(resultSet
				.getString(COLUME_LICENSESTATE)));
		user.setLicenseStateString(LicenseState.valueOf(resultSet
				.getString(COLUME_LICENSESTATE)).getValue());
		user.setActivity(Activity.valueOf(resultSet.getString(COLUME_ACTIVITY)));
		user.setStringactivity(Activity.valueOf(resultSet.getString(COLUME_ACTIVITY)).getValue());
		user.setRole(Role.valueOf(resultSet.getString(COLUME_ROLE)));
		user.setMajor(resultSet.getString(COLUME_MAJOR));
		user.setExperience(resultSet.getString(COLUME_EXPERIENCE));
		user.setIncome(resultSet.getString(COLUME_INCOME));
		user.setBirthday(resultSet.getDate(COLUME_BIRTHDAY));
		user.setBirthdayString(DateFormater.changeToString(resultSet.getTimestamp(COLUME_BIRTHDAY),"yyyy-MM-dd HH:mm"));
		user.setBloodType(BloodType.valueOf(resultSet
				.getString(COLUME_BLOODTYPE)));
		user.setStringbloodType(BloodType.valueOf(resultSet
				.getString(COLUME_BLOODTYPE)).getValue());
		user.setSafeFactor(SafeFactor.valueOf(resultSet
				.getString(COLUME_SAVEFACTORY)));
		user.setVip(VIP.valueOf(resultSet.getString(COLUME_VIP)));
		user.setCareCount(resultSet.getInt(COLUME_CARECOUNT));
		user.setFansCount(resultSet.getInt(COLUME_FANCOUNT));
		return user;
	}

}
